package database;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class DP {
	private static final String URL = 
	"jdbc:mysql://localhost:3306/myproject1"
	+ "?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true";
	private static final String USER = "root";
	private static final String PASSWORD = "xzl0314";
	public static Connection conn = null;
	public static PreparedStatement ps = null;
	public static ResultSet rs = null;
	
	public static void init() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
	}
	
	public static void close() {
		try {
			if(rs!=null) rs.close();
			if(ps!=null) ps.close();
			if(conn!=null) conn.close();
			System.out.println("数据库关闭成功");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
	}
	public static <T> List<T> fun1(Class<T> c,String sql) throws Exception{
		ps = conn.prepareStatement(sql);
		System.out.println(ps);
		rs = ps.executeQuery();
		String[] names = {"id","cname","mname"};
		List<T> list = new ArrayList<T>();
		ResultSetMetaData rsd = rs.getMetaData();
		while(rs.next()) {
			T t = c.newInstance();
			//System.out.println("共有页"+rsd.getColumnCount());
			for(int i = 0; i < rsd.getColumnCount(); i++) {
				String name = rsd.getColumnName(i+1);
				//System.out.println("行名是"+name);
				Object o = rs.getObject(name);
				//System.out.println("值o是"+o);
				Field f = c.getDeclaredField(names[i]);
				f.setAccessible(true);
				f.set(t, o);
			}
			list.add(t);
		}
		return list;
	}
	public static <T> List<T> update(Class<T> c,String sql,Object...paras){                 
		try {
			ps = conn.prepareStatement(sql);
			if(paras!=null) {
				for(int i = 0; i < paras.length; i++) {
					ps.setObject(i+1, paras[i]);
				}				
			}
			System.out.println(ps);
			if(sql.substring(0, 6).equals("select")) {
				rs = ps.executeQuery();
				List<T> list = new ArrayList<T>();
				ResultSetMetaData rsd = rs.getMetaData();
				while(rs.next()) {
					T t = c.newInstance();
					for(int i = 0; i < rsd.getColumnCount(); i++) {
						String name = rsd.getColumnName(i+1);
						Object o = rs.getObject(name);
						try {
							Field f = c.getDeclaredField(name);
							f.setAccessible(true);
							f.set(t, o);
						} catch (Exception e) {
							System.out.println("获取变量失败");
							e.printStackTrace();
						}
					}
					list.add(t);
				}
				return list;
			}
			else {
				String s1 = c.getName();
				int idex = s1.lastIndexOf(".");
				String s2 = s1.substring(idex+1, s1.length());
				ps.executeUpdate();
				return update(c, "select * from "+s2.toLowerCase());
			}
		} catch (Exception e) {
			System.out.println("更新失败");
			e.printStackTrace();
			return null;
		}	
	}
	
	public static void main(String[] args) {
		init();
		String sql2 = "insert into college(id,name) values(?,?)";
		String sql3 =" insert into college(id,name) values(7,'ww')";
		try {
			String sql = "select * from student";
			for ( Object o : update(Student.class,sql)) {
				System.out.println(o);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			close();			
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
